package demo.pattern.singleton;

/**
 * @Author: yu.zhou
 * @DateTime: 2023/01/04 13:54
 * <p>枚举饿汉单例模式</p>
 * <p>
 * 反射无法破除防御
 */
public class EnumStarvingSingleton {

    private EnumStarvingSingleton() {
    }

    public static EnumStarvingSingleton getInstance() {
        return ContainHolder.HOLDER.instance;
    }

    /**
     * 为了破除反射对私有对象的影响使用枚举类来持有实例
     */
    private enum ContainHolder {
        /**
         * 保存单例实例
         */
        HOLDER;

        private EnumStarvingSingleton instance;

        ContainHolder() {
            instance = new EnumStarvingSingleton();
        }
    }
}
